/*
 * @Author: xiaosihan
 * @Date: 2024-07-02 11:57:47
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2025-01-21 18:42:16
 */
import { Scene } from '@babylonjs/core/scene';
import { LinesMesh } from '@babylonjs/core/Meshes/linesMesh';
import { Vector3 } from '@babylonjs/core/Maths/math.vector';
import { Color4 } from '@babylonjs/core/Maths/math.color';
import { Node } from '@babylonjs/core/node';
import { CreateLineSystemVertexData } from '@babylonjs/core/Meshes/Builders/linesBuilder';

// 三维轴线
export default class Axis extends Node {
  constructor(length: number = 10, scene: Scene) {
    super('axis', scene, true);

    this.x_axis = new LinesMesh('x_axis', scene, this);
    this.y_axis = new LinesMesh('y_axis', scene, this);
    this.z_axis = new LinesMesh('z_axis', scene, this);

    this.init(length);
  }

  x_axis!: LinesMesh;
  y_axis!: LinesMesh;
  z_axis!: LinesMesh;

  init(length: number = 10) {
    // 创建x 轴线
    const x_vertexData = CreateLineSystemVertexData({
      lines: [
        [new Vector3(0, 0, 0), new Vector3(length, 0, 0)],
        // 创建x 刻度线
        ...new Array(length * 10).fill(0).map((v, i) => {
          let width = i % 10 === 0 ? 0.3 : 0.1;
          width = i === 0 ? 0 : width;
          return [new Vector3(i / 10, 0, -width), new Vector3(i / 10, 0, width)];
        }),
      ],
      colors: [
        [new Color4(1, 0, 0, 1), new Color4(0.5, 0, 0, 1)],
        ...new Array(length * 10).fill(0).map((v) => {
          return [new Color4(1, 0, 0, 1), new Color4(1, 0, 0, 1)];
        }),
      ],
    });
    // 应用到x轴模型对象上
    x_vertexData.applyToMesh(this.x_axis);

    // 创建y 轴线
    const y_vertexData = CreateLineSystemVertexData({
      lines: [
        [new Vector3(0, 0, 0), new Vector3(0, length, 0)],
        // 创建y 刻度线
        ...new Array(length * 10).fill(0).map((v, i) => {
          let width = i % 10 === 0 ? 0.3 : 0.1;
          width = i === 0 ? 0 : width;
          return [new Vector3(width, i / 10, -width), new Vector3(-width, i / 10, width)];
        }),
      ],
      colors: [
        [new Color4(0, 1, 0, 1), new Color4(0, 0.5, 0, 1)],
        ...new Array(length * 10).fill(0).map((v) => {
          return [new Color4(0, 1, 0, 1), new Color4(0, 1, 0, 1)];
        }),
      ],
    });
    // 应用到y轴模型对象上
    y_vertexData.applyToMesh(this.y_axis);

    // 创建z 轴线
    const z_vertexData = CreateLineSystemVertexData({
      lines: [
        [new Vector3(0, 0, 0), new Vector3(0, 0, length)],
        // 创建z刻度线
        ...new Array(length * 10).fill(0).map((v, i) => {
          let width = i % 10 === 0 ? 0.3 : 0.1;
          width = i === 0 ? 0 : width;
          return [new Vector3(-width, 0, i / 10), new Vector3(width, 0, i / 10)];
        }),
      ],
      colors: [
        [new Color4(0, 0, 1, 1), new Color4(0, 0, 0.5, 1)],
        ...new Array(length * 10).fill(0).map((v) => {
          return [new Color4(0, 0, 1, 1), new Color4(0, 0, 1, 1)];
        }),
      ],
    });
    // 应用到z轴模型对象上
    z_vertexData.applyToMesh(this.z_axis);
  }
}
